/*
©2001-2005 Haiku - http://haiku-os.org/

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.

		File Name:	html2dat.cpp
		Description:	machiBBSのhtmlファイルをdatファイル形式に変換する
							<dt>....<dd>  ...   \n
*/
#include	<stdio.h>
#include	<DataIO.h>
#include	<File.h>
#include	<String.h>
#include	<string.h>	// strncpy

#include "html2dat.h"

#define		HTML2DAT_SIZE			8192

////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
void
html2dat::machi_dat(char *data, int32 length) /* strdup()してもいいのだが続いて使わないのでdataは破壊する。 */
{
	const char *name = NULL;
	const char *email = NULL;
	const char *date = NULL;
	const char *contents = NULL;
	const char *title = NULL;

	int32 s = 4;	/* 先頭の<dt>をパス */
	while (!name && s < length) {	/* name, e-mail */
		if (data[s] == 'm' && strncasecmp(data + s, "mailto:", 7) == 0) {
			email = data + s + 7;
			s += 7; /* "が無いとまずいのだが*/
			while (s < length){
				if (data[s] == '"') {
					data[s++] = '\0';
					break;
				}else{
					s++;
				}
			}
		}else if (data[s] == '<' && strncasecmp(data + s, "<b>", 3) == 0){ 
			name = data + s + 4;
			s += 4; /* </b>が無いとまずいのだが*/
			//東京２３区板など、<font color="#008800"><b> <font color=#FF0000>名前</font> </b></font>の場合。
			if (data[s] == '<'){
				while (data[s] != '>') s++;
				s++;
				name = data + s;
			}

			while (s < length){
				if (data[s] == ' ' && strncasecmp(data + s, " </b>", 5) == 0) {
					data[s] = '\0';
					s += 5;
					break;
				//東京２３区板など、<font color="#008800"><b> <font color=#FF0000>名前</font> </b></font>の場合。
				}else if (data[s] == '<' && strncasecmp(data + s, "</font> </b>", 12) == 0) {
					data[s] = '\0';
					s += 12;
					break;
				}else{
					s++;
				}
			}
		}else{
			s++;
		}

	}

	//投稿日：(SJIS)
	const uint8 post[] = { 0x93, 0x8a, 0x8d, 0x65, 0x93, 0xfa, 0x81, 0x46, 0x20, 0};
	while (!date && s < length) { /* date */
		if (data[s] == ' ' && strncmp(data + s + 1, (const char*)post, 9) == 0) {
			date = data + s + 10;
			s += 10; // 日付でないものがあったので 23; /* yyyy/mm/dd() hh:mm:ss*/
			while (s < length)
				if (data[s] == '<') {
					data[s] = '\0';
					s += 4;
					break;
				}else{
					s++;
				}
		}else{
			s++;
		}
	}

	while (s < length) { /* contents */
		if (data[s] == '<' && strncasecmp(data+s, "<dd>", 4) == 0) {
			contents = data + s + 4;
			data[length - 1] = '\0';
			break;
		}else{
			s++;
		}
	}
	BMallocIO::Write(name, strlen(name));
	BMallocIO::Write("<>", 2);
	if (email) BMallocIO::Write(email, strlen(email));
	BMallocIO::Write("<>", 2);
	if (date) BMallocIO::Write(date, strlen(date));
	BMallocIO::Write("<>", 2);
	if (contents){
		for (size_t s = 0; s < strlen(contents); s++){
		// <a href....>,および </a>を削除。
		if(contents[s] == '<'){		
			if(strncasecmp(contents + s, "</a>", 4) == 0){
				s += 4;
			}else if(strncasecmp(contents + s, "<a href", 7) == 0){
				while(contents[s] != '>') s++;
				s++;
			}
		}
		
		BMallocIO::Write(contents + s , 1);
	}

	BMallocIO::Write("<>", 2);
	if (title) Write(title, strlen(title));
	BMallocIO::Write("\n", 1);
//	/* for DEBUG */
//	if (date[0] == 0 || contents[0] == 0) {
//		data[length-1] = '\0';
//		printf("Warning(%s)\n", data);
//		if (name[0]) printf("name(%s)\n", name);
//		if (email[0]) printf("email(%s)\n", email);
//		if (date[0]) printf("date(%s)\n", date);
//		if (contents[0]) printf("contents(%s)\n", contents);
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
html2dat::html2dat(BPositionIO *file, bool overwrite)
	: BMallocIO()
{
	file->Seek(0, SEEK_SET);
	char data[HTML2DAT_SIZE];
	int32 left = 0, effect = 0;
	while (0 < (effect = left+file->Read(data+left, HTML2DAT_SIZE-left))) {
		int32 used = 0;
		
aa:	for (int32 i = used; i < effect; i++)
			if (data[i] == '<' && (4 < effect - i) && strncmp(data + i, "<dt>", 4) == 0) {
				used = i;
				left = effect - used;
				if (0 < left) memcpy(data, data+used, left);
				while (0 < (effect = left+file->Read(data + left, HTML2DAT_SIZE - left))) {
					for (int32 j = 4; j < effect; j++){
						if (data[j] == '\n' && data[j + 1] != '<' ){
							data[j] = ' ';
						}else if (data[j] == '\n' && data[j + 1] == '<' &&
										(data[j + 2] == 'd' || data[j + 2] == 't')) {
							this->machi_dat(data, j + 1);
							used = j + 1;
							goto aa;
						}
					}
					data[HTML2DAT_SIZE - 1] = '\0';
					//printf("\nPROBLEM\n%s", data);
					return;
				/* \nが見付からない。あるいは結構(HTML2DAT_SIZE以上)長い */
				}
			}
		used = effect;	/* "<"が見付からなかった */
		left = effect - used;
		if (0 < left) memcpy(data, data + used, effect - used);
	}
	if (overwrite) this->Clone(file);
	Seek(0, SEEK_SET);
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
void
html2dat::Clone(BPositionIO *file)
{
	file->Seek(0, SEEK_SET);
	file->Write(Buffer(), BufferLength());
	file->SetSize(BufferLength());
	file->Seek(0, SEEK_SET);
}
